import 'dart:io';
import 'package:encrypt/encrypt.dart';
import 'package:encrypt/encrypt_io.dart';
import 'package:pointycastle/asymmetric/api.dart';
import 'dart:convert';


import 'dart:async' show Future;
import 'package:flutter/services.dart' show rootBundle;



class RSATool{

//  static Future rsaEncode (String plainText){
//
//  }
//
//  final publicKey = await parseKeyFromFile<RSAPublicKey>('test/public.pem');
//  final privKey = await parseKeyFromFile<RSAPrivateKey>('test/private.pem');
//
//  final plainText = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit';
//  final encrypter = Encrypter(RSA(publicKey: publicKey,));
//
//  final encrypted = encrypter.encrypt(plainText);
//  final decrypted = encrypter.decrypt(encrypted);


//  //公钥
//  static const _publicKeyString = "MIGeMA0GCSqGSIb3DQEBAQUAA4GMADCBiAKBgQDrKjhWhmGIf6GAvdtcq9XyHHv9WcCQyy0kWoesJTBiiCcpKT5VBjUFCOf5qju3f0MzIxSQ+RX21jxV/i8IpJs1P0RK05k8rMAtt4Sru45CqbG7//s4vhjXjoeg5Bubj3OpKO4MzuH2c5iEuXd+T+noihu+SVknrEp5mzGB1kQkQwICJxE=";
//
//  //密钥
//  static const _privateKeyString = "91115898314626172575780572748811022866127280528501719435666944855512659305508706741636526635072837330438002115090887976360694140207465899834079287783735083201083139689099625625275152853382823941160163213363646601509563713397941330507586212418844227184998461863365846964374270885254766622682405389783755935703";
//
//
//  static Future<String> rsaEncode (String base64PlainText, {String publicKey=_publicKeyString}) async{
//    var parser = RSAKeyParser();
//    String rsaPublic = "-----BEGIN PUBLIC KEY-----\n$publicKey\n-----END PUBLIC KEY-----";
//    RSAPublicKey rsaPublicKey = parser.parse(rsaPublic);
//    // String rsaTxt =  await crypto.DYFCryptoProvider.rsaEncrypt(plainText, key);
////    return Encrypter(RSA(publicKey: rsaPublicKey)).encrypt(base64PlainText).base64;
//    return Encrypter(RSA(publicKey: rsaPublicKey)).encrypt(base64PlainText).base64 ;
//  }
//
//
//  //base64PlainText base64处理的密文
//  static Future<String> rsaDecode (String base64PlainText, {String privateKey=_privateKeyString}) async{
//    var parser = RSAKeyParser();
//    String rsaPublic = "-----BEGIN PRIVATE KEY-----\n$privateKey\n-----END PRIVATE KEY-----";
//    RSAPrivateKey rsaPrivateKey = parser.parse(rsaPublic);
//    // String rsaTxt =  await crypto.DYFCryptoProvider.rsaEncrypt(plainText, key);
//    return Encrypter(RSA(privateKey: rsaPrivateKey)).decrypt64(base64PlainText);
//  }
//
//
//  static String urlEncode(String component){
//    return Uri.encodeComponent(component);
//  }
//
//  static String urlDecode(String component){
//    return Uri.decodeComponent(component);
//  }



//  static  const plainText = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit';
//  static const key = new Key.fromUtf8('my32lengthsupersecretnooneknows1');
//  static const iv = IV.fromLength(16);
//
//  final b64key = Key.fromUtf8(base64Url.encode(key.bytes));
//  // if you need to use the ttl feature, you'll need to use APIs in the algorithm itself
//  final fernet = Fernet(b64key);
//  final encrypter = Encrypter(fernet);
//
//  final encrypted = encrypter.encrypt(plainText);
//  final decrypted = encrypter.decrypt(encrypted);

//  static const key = new Key.fromUtf8();
//  static const iv = IV.fromLength(16);


//
//  static String encode(String plainText){
//
////    final  key =  Key.fromUtf8('ca56aa9d90e438b659c4d8da8d586687eabe1ba7bd90463981ea5397aab90020387e0a541020eddaa746f982a30115c54b04d5d1f823345f8d16ebffe647c986be3692158fc08413854ab3123b48c9ff486b12905ab21dd2dcfbbab9a2afac89953d86582bd13392c8fbbba0795fb00ebfffe3b0eb6a9cc372ee84635984807f');
////    final  iv = IV.fromUtf8('10001');
//
//    final key = Key.fromUtf8('my32lengthsupersecretnooneknows1');
//    final iv = IV.fromLength(16);
//
//    final b64key = Key.fromUtf8(base64Url.encode(key.bytes));
//    // if you need to use the ttl feature, you'll need to use APIs in the algorithm itself
//    final fernet = Fernet(b64key);
//    final encrypter = Encrypter(fernet);
////
//    final encrypted = encrypter.encrypt(plainText,iv: iv);
//    return   encrypted.base64;
//  }

  static Future<String> loadAsset(String path) async {
    return await rootBundle.loadString(path);
  }


  static  encode(String plainText)  async{

    String publicKeyString = await rootBundle.loadString('RootResource/uxin_login_pub_key.pem');
    print('publicKeyString------------$publicKeyString');
    var parser = RSAKeyParser();
    RSAPublicKey publicKey = parser.parse(publicKeyString);

    final encrypter = Encrypter(RSA(publicKey: publicKey,));
    final encrypted = encrypter.encrypt(plainText);

    return encrypted.base16;

  }


}